package com.zzy.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.zzy.commonutils.R;
import com.zzy.servicebase.exceptionhandler.GuliException;
import com.zzy.vod.service.VodService;
import com.zzy.vod.utils.ConstantVodUtils;
import com.zzy.vod.utils.InitVodClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @Author KAZU
 * @Date 2023/1/16 12:06
 * @Version 1.0
 */
@SuppressWarnings({"all"})
@Service
public class VodServiceImpl implements VodService {
	
	/**
	 * @param:accessKeyId
	 * @param:accessKeySecret
	 * @param:title————上传后显示名称
	 * @param:fileName————上传文件原始名称
	 * @param:inputStrea————上传文件输入流
	 * */
	@Override
	public String uploadVideoAly(MultipartFile file) throws IOException {
		String accessKeyId = ConstantVodUtils.ACCESS_KEY_ID;
		String accessKeySecret = ConstantVodUtils.ACCESS_KEY_SECRET;
		String fileName = file.getOriginalFilename();
		String title = fileName.substring(0, fileName.lastIndexOf("."));
		InputStream inputStream = file.getInputStream();
		String videoId = "";
		UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, inputStream);
		UploadVideoImpl uploader = new UploadVideoImpl();
		UploadStreamResponse response = uploader.uploadStream(request);
		videoId = response.getVideoId();
		return videoId;
	}
	
	/**
	 * 删除多个阿里云视频的方法
	 * */
	@Override
	public R removeMoreAlyVideo(List videoIdList) {
		try {
			// 初始化对象
			DefaultAcsClient client = InitVodClient.initVodClient(
					ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
			// 创建删除视频request对象
			DeleteVideoRequest request = new DeleteVideoRequest();
			
			String videoIds = StringUtils.join(videoIdList.toArray(), ",");
			
			// 向request 设置视频id
			request.setVideoIds(videoIds);
			// 调用初始化对象的方法实现删除
			client.getAcsResponse(request);
			return R.ok();
		} catch (ClientException e) {
			e.printStackTrace();
			throw new GuliException(20001, "删除视频失败");
		}
	}
	
	
}
